package com.jiyongjun.bms.base.common.core;

import com.github.pagehelper.PageHelper;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <h2>服务类公共实现类</h2>
 * description:
 *
 * @author yongjun.ji
 * @since 1.0 2019/5/31
 */
public abstract class BaseServiceImpl<E extends BaseEntity, Q extends BaseQueryModel, K extends Serializable> implements BaseService<E, Q, K> {

    protected abstract BaseMapper<E> getMapper();

    @Override
    public E getById(K id) {
        return getMapper().selectByPrimaryKey(id);
    }

    @Override
    public List<E> getByCondition(Q queryModel) {
        if (queryModel.getPageSize() != null && queryModel.getPageNo() != null) {
            PageHelper.startPage(queryModel.getPageNo(), queryModel.getPageSize());
        }
        return null;
    }

    @Override
    public E insert(E entity) {
        entity.setCreateTime(new Date());
        getMapper().insertSelective(entity);
        return entity;
    }

    @Override
    public Integer update(E entity) {
        entity.setModifiedTime(new Date());
        return getMapper().updateByPrimaryKey(entity);
    }

    @Override
    public Integer patchUpdate(E entity) {
        entity.setModifiedTime(new Date());
        return getMapper().updateByPrimaryKeySelective(entity);
    }

    @Override
    public Integer delete(K id) {
        return getMapper().deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteLogically(K id) {
        throw new UnsupportedOperationException("暂不支持该操作");
    }
}
